package cn.anecansaitin.not_enough_harness.common.capability.harness_capability;

import cn.anecansaitin.not_enough_harness.common.harness_data.HarnessCapabilityCD;
import cn.anecansaitin.not_enough_harness.common.harness_data.RushTriggerHarness;
import cn.anecansaitin.not_enough_harness.common.harness_data.RushTriggerHarnessDirection;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.animal.HappyGhast;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;

public class RushTriggerHarnessHandler implements HarnessCapabilityHandler {
    public static final RushTriggerHarnessHandler INSTANCE = new RushTriggerHarnessHandler();

    @Override
    public void tick(ItemStack harness, HappyGhast happyGhast, LivingEntity driver) {
        HarnessCapabilityCD harnessCapabilityCD = HarnessCapabilityCD.get(harness);

        if (harnessCapabilityCD.duration() > 0) {
            RushTriggerHarnessDirection rushTriggerHarnessDirection = RushTriggerHarnessDirection.get(harness);
            happyGhast.setDeltaMovement(rushTriggerHarnessDirection.direction());
            HarnessCapabilityCD.set(harness, harnessCapabilityCD.cd(), harnessCapabilityCD.duration() - 1);
        } else if (harnessCapabilityCD.cd() > 0) {
            HarnessCapabilityCD.set(harness, harnessCapabilityCD.cd() - 1, 0);
        }
    }

    @Override
    public void use(ItemStack harness, HappyGhast happyGhast, LivingEntity driver) {
        HarnessCapabilityCD capabilityCD = HarnessCapabilityCD.get(harness);

        if (capabilityCD.cd() > 0) {
            return;
        }

        RushTriggerHarness rushTriggerHarness = RushTriggerHarness.get(harness);
        HarnessCapabilityCD.set(harness, rushTriggerHarness.cd(), rushTriggerHarness.duration());
        float yHeadRot = driver.getYHeadRot();
        Vec3 direction = rushTriggerHarness.direction();
        Vec3 vec3 = (direction.lengthSqr() > 1.0 ? direction.normalize() : direction).scale(rushTriggerHarness.speed());
        float f = Mth.sin(yHeadRot * Mth.DEG_TO_RAD);
        float f1 = Mth.cos(yHeadRot * Mth.DEG_TO_RAD);
        RushTriggerHarnessDirection.set(harness, new Vec3(vec3.x * f1 - vec3.z * f, vec3.y, vec3.z * f1 + vec3.x * f));
    }
}
